<?php
require_once 'Site.php';
require_once 'NavigationTree.php';
class SiteEdit
{
	/**
	 * Datenbank verbindung
	 *
	 * @var Kanedo_Db
	 */
	protected $_db = null;
	/**
	 * Seitenmodel
	 *
	 * @var Site
	 */
	protected $_site = null;
	/**
	 * Navigationmodel
	 *
	 * @var NavigationTree
	 */
	protected $_navi = null;
	
	/**
	 * Konstruktor
	 *
	 * @param Kanedo_Db $db
	 * @param string $tableNavigation
	 */
	public function __construct(Kanedo_Db $db, $tableNavigation)
	{
		$this->_db = $db;
		if (!$this->_db->isConnected())
		{
			throw new Kanedo_Db_Exception("No connection to DB found!");
		}
		$this->_site = new Site($this->_db);
		$this->_navi = new NavigationTree($this->_db, 'navigation');
		
	}
	public function configure($config)
	{
		/**
		 * @todo Konfigurierbar machen
		 */
	}
	
	/**
	 * @param $id 
	 * @param $title
	 * @param $content
	 * @param $date
	 * @param $user
	 * @param $published
	 * @return unknown_type
	 */
	public function editSite ($id, $title,  $content,  $date,  $user,  $published)
	{
		$this->_site->updateSite($id, $title,  $content,  $date,  $user,  $published);
		$this->_navi->updateNodeById($id, array('name' => $title));
		
	}
	
	/**
	 * Neue Site erstellen
	 *
	 * @param string $title
	 * @param string $content
	 * @param timestamp $date
	 * @param int $user
	 * @param bool $published
	 * @param int $parent
	 * @return int $id
	 */
	public function createChildSite($title,  $content,  $date,  $user,  $published, $parent)
	{
		 $title = $this->_db->escape($title);
		 $content = $this->_db->escape($content);
		 $date = $date;
		 $published = ($published)? 1 : 0;
		require_once 'User.php';
		$ObUser = new User($this->_db);
		$ObUser = $ObUser->getUserById($user);
		$id = $this->_site->insertSite($title, $content, $date, $ObUser['id_user_user'], $published);
		$this->_navi->insertChildNode($title,$id, $parent);
		return $id;
	}
	
	public function getSiteById($id)
	{
		$data = $this->_site->getSiteById($id);
		if (count($data) > 1)
		{
			throw new Kanedo_Model_Exception("Request returns an not unique entry!");
		}
		return $data[0];
	}
}
?>